let count = 10; //每一页的条数
let page = 1; //当前页
//渲染页面
showData()
function showData() {
  $.ajax({
    url: '/renshi/query',
    data: {
      count: count,
      page: page
    },
    success: (res) => {
      if (res.error) {
        console.log(res.erro);
      } else {
        // 渲染表格
        $('tbody').html('')
        $(res.data).each((index, item) => {
          $('tbody').append(`
                    <tr data-id="${item.y_id}">             
                        <td>${item.y_id}</td>
                        <td>${item.y_bumen}</td>
                        <td>${item.y_name}</td>
                        <td>${item.y_youxiang}</td>
                        <td>${item.y_gonghao}</td>
                        <td>${item.y_ruzhi}</td>
                        <td>${item.y_gonghao}</td>
                        <td>${item.y_ruzhi}</td>
                        <td>
                        <button class="mod"><span class="iconfont icon-pen"></span>编辑</button>
                        <button class="del"><span class="iconfont icon-lajitong"></span>删除</button>
                        </td>

                    </tr>
                    `);
        });
        $('#myli').html('')
        maxpage = Math.ceil((res.total / count))
        for (let i = 1; i <= Math.ceil((res.total / count)); i++) {
            if (page == i) {
                $('#myli').append(`
                        <a id="a" class="active new_page" href="javascript:;">${i}</a>
                                `)
            } else {
                $('#myli').append(`
                <a href="javascript:;" id="a">${i}</a>
               `)
            }
        }
      }
    }
  });
};
//查询搜索
$('.query').click(() => {
  console.log($('#number input[type=text]').val())
  $.ajax({
      url: '/renshi/sousuo',
      method: 'post',
      data: {
          number: $('#number input[type=text]').val(),
          name: $("#number input[type=text1]").val(),
      },
      success: (res) => {
          if (res.error) {
              console.log(res.error);
          } else {
              // 渲染表格
              $('tbody').html('')
              $(res.data).each((index, item) => {
                  $('tbody').append(`
                  <tr data-id="${item.y_id}">             
                        <td>${item.y_id}</td>
                        <td>${item.y_bumen}</td>
                        <td>${item.y_name}</td>
                        <td>${item.y_youxiang}</td>
                        <td>${item.y_gonghao}</td>
                        <td>${item.y_ruzhi}</td>
                        <td>${item.y_gonghao}</td>
                        <td>${item.y_ruzhi}</td>
                        <td>
                        <button class="mod"><span class="iconfont icon-pen"></span>编辑</button>
                        <button class="del"><span class="iconfont icon-lajitong"></span>删除</button>
                        </td>

                    </tr>
                  `);
              });
              // 渲染页码
              $('#myli').html('')
              maxpage = Math.ceil((res.total / count))
              for (let i = 1; i <= Math.ceil((res.total / count)); i++) {
                  if (page == i) {
                      $('#myli').append(`
                              <a id="a" class="active" href="javascript:;">${i}</a>
                                      `)
                  } else {
                      $('#myli').append(`
                      <a href="javascript:;" id="a">${i}</a>
                     `)
                  }
              }
          }
      }
  })
})
//点击上一页
$('.pagination').on('click', '.page_left', function () {
  // console.log($(this).text())
  if (page > 1) {
      page--
  } else {
      page = 1
  }
  showData()
})

//点击下一页
$('.pagination').on('click', '.page_right', function () {
  // console.log($(this).text())
  if (page != maxpage) {
      page++
  } else {
      page = maxpage
      console.log(maxpage)
  }
  showData()
})
//删除
$('tbody').on('click', '.del', function () {
  console.log($(this).parents('tr').attr('data-id'))
  if (confirm('确认删除这条数据吗？')) {
      $.ajax({
          url: '/renshi/del',
          method: 'post',
          data: {
              id: $(this).parents('tr').attr('data-id'),
          },
          success: (res) => {
              if (res.error) {
                  console.log(res.msg)
              } else {
                  // alert('确认删除这条数据吗？')
                  showData();
              }
          }
      })
  }
})
//点击切换页码
$('.pagination').on('click', '#a', function () {
  console.log($(this).text());
  page = $(this).text();
  showData();
});
//重置
$('.reset').click(() => {
  showData()
})
//修改
let id
let number
$('tbody').on('click', '.mod', function () {
    location.href='../../pages/renshiguanli/chuangjianlizhiyuanyin.html'
    id = $(this).parents('tr').attr('data-id');
    number = JSON.parse($(this).attr('data-number'));
    console.log(number)
    console.log(id);
   
    localStorage.setItem('id', JSON.stringify(id))
    localStorage.setItem('number', JSON.stringify(number))
})
  
